package com.hrbxlh.actionImpl;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import com.hrbxlh.util.SessionValues;
import com.hrbxlh.util.SessonMapUtil;
import com.hrbxlh.virtualization.enums.LoginActionEnum;
import com.hrbxlh.virtualization.util.IpUtil;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.hrbxlh.dao.LoginDao;
import com.hrbxlh.util.PubMethods;


@Component("loginActionImpl")
@Transactional
public class LoginActionImpl {
	
	@Resource(name = "loginDao")
	private LoginDao loginDao;

	@Autowired
	private MemberActionImpl memberActionImpl;

//	@Autowired
//	private SysUserLoginHistoryMapper sysUserLoginHistoryMapper;

	@Autowired
	private ThreadPoolTaskExecutor taskExecutorTwo;
	
	/**
	 * @description 更改登陆退出时间
	 *
	 */
	public boolean upLogin(HttpSession session, HttpServletRequest request) throws Exception {

		boolean flag = false;
		Map<String, Object> paramMap = new HashMap<String,Object>();
		try {
			SessonMapUtil sessonMapUtil = new SessonMapUtil();
			String loginoutTime = PubMethods.getNowTime(new Date(),1);
			paramMap.put("loginoutTime", loginoutTime);
			int mainId = 0;
			Map<String, Object> sessionMap = new SessionValues().getSessionValues(new String[] {"mainId","stuorteaId"}, request, "userMsg");
			if(sessionMap!=null&&sessionMap.size()!=0){
				mainId = Integer.parseInt(String.valueOf(sessionMap.get("mainId")));
			}else if(sessionMap==null&&sessonMapUtil.getSessionMap()!=null&&sessonMapUtil.getSessionMap().size()!=0){
				mainId = Integer.parseInt(String.valueOf(sessonMapUtil.getSessionMap().get("mainId")));
			}else if(sessionMap==null&&sessonMapUtil.getSessionMap()==null){
				return true;
			}
			paramMap.put("mainId",mainId);
			 //查询当前登录人此次退出的信息
			Map<String,Object> loginMap = loginDao.getLoginInfoByMainId(paramMap);
			if(loginMap!=null&&loginMap.size()!=0){
				//将退出信息插入到登陆or退出历史表中
				String ip = IpUtil.getIpAddr(request);
				Map<String,Object> getMap = memberActionImpl.getMemberByStuorteaId(String.valueOf(sessionMap.get("stuorteaId")));
				Map<String,Object> userMap = (Map<String, Object>) getMap.get("memberMap");
//				SysUserLoginHistory sysUserLoginHistory = new SysUserLoginHistory();
//				sysUserLoginHistory.setAction(LoginActionEnum.LOGOUT.getCode());
//				sysUserLoginHistory.setUserId(Integer.parseInt(String.valueOf(userMap.get("userNum"))));
//				sysUserLoginHistory.setUserName(String.valueOf(userMap.get("userName")));
//				sysUserLoginHistory.setUserNumber(String.valueOf(userMap.get("stuorteaId")));
//				sysUserLoginHistory.setLoginIp(ip);
//				sysUserLoginHistoryMapper.insertSelective(sysUserLoginHistory);
//				taskExecutorTwo.execute(new TaskRunnable(sysUserLoginHistory));
				/*List<SysUserLoginHistory> sysUserLoginHistories = sysUserLoginHistoryMapper.getSysLoginHistoryByIp(sysUserLoginHistory);
				sysUserLoginHistory.setCountry(String.valueOf(sysUserLoginHistories.get(0).getCountry()));
				sysUserLoginHistory.setRegion(String.valueOf(sysUserLoginHistories.get(0).getRegion()));
				sysUserLoginHistory.setCity(String.valueOf(sysUserLoginHistories.get(0).getCity()));*/

				String loginTime = String.valueOf(loginMap.get("loginTime"));
				long startTime = PubMethods.getTimeSeconds(loginTime);
				long endTime = PubMethods.getTimeSeconds(loginoutTime);
				long chaTime = endTime-startTime;
				//将毫秒数转换为小时
				BigDecimal stayTime = (new BigDecimal(chaTime).divide(new BigDecimal(1000),10,BigDecimal.ROUND_HALF_UP)).divide(new BigDecimal(3600),10,BigDecimal.ROUND_HALF_UP);
				paramMap.put("stayTime",stayTime);
				loginDao.upLogin(paramMap);
			}
			//清空session
			session.removeAttribute("userMsg");

		} catch (Exception e) {
			throw e;
		}
		return flag;
	}

	public long getMaxPrimId() throws Exception{
		long mainId = 0;
		try {

			mainId = loginDao.getMaxPrimId();
		}catch (Exception e){
			e.printStackTrace();
		}
		return mainId;
	}

}
